package com.qfly.main.common.jpa.audit;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Optional;

/**
 * Wisdytech.cn
 * com.qfly.main.common.jpa.jpa.audit.qflyAuditorAware
 *
 * @author hailin.zhl
 * @date 2018/12/07
 */
@Configuration
public class QflyAuditorAware implements AuditorAware<String>{


    @Override
    public Optional<String> getCurrentAuditor() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth == null || !auth.isAuthenticated()) {
            return Optional.empty();
        }
        return Optional.of(((UserDetails)auth.getPrincipal()).getUsername());
    }
}
